import SendMsg from "@/api/SendMsg";
import {message} from "ant-design-vue";
import i18n from "../../../i18n/index";
import router from "@/router";
import { cloneDeep } from 'lodash';

const barTender = {
    namespaced: true,
    state: {
        addOrEdit: "",
        delFlg: "",
        aa: [
            {
                productName: "",
            }
        ],
        typeList: [],
        valueList: [],
        dateList: [],
        stringValueList: [],
        templateNameDisabled: true,
        selectedData: [],
        addOrEditData: [],
        delData: [],
        addData: [],
        editData: [],
        updateTemplateData: [],
        addOrEditTemplate: {
            id: "",
            type: "",
            parameter: "",
            value: ""
        },
        binding: {
            templateName: "",
            product: "",
            customerCode:""
        },
        selectTemplateName: "",
        bindIngfSelectData: {

        },
        templateDataSource: [],
        productDataSource: [],
        userDataSource:[],
        templateVisible: false,
        bindingVisible: false,
        template: {
            name: ""
        },
        templateList: {
            name: "",
            id: ""
        },
        labelCol: {
            span: 8
        },
        wrapperCol: {
            span: 16
        },
        rowSelection: {
            selectedRowKeys: [],
            type: 'radio',
            onChange: (selectedRowKeys, selectedRows) => {
                barTender.mutations.queryTemplateName(selectedRows[0].templateName);
                barTender.state.rowSelection.selectedRowKeys = selectedRowKeys;
                barTender.state.templateNameDisabled = true;
                barTender.state.templateList.name = selectedRows[0].templateName;
            },
        },
        productRowSelection: {
            selectData: [],
            selectedRowKeys: [],
            type: 'checkbox',
            onChange: (selectedRowKeys, selectedRows) => {
                console.log('productRowSelection ---> '+selectedRowKeys)
                console.log('productRowSelection ---> '+selectedRows)
                barTender.state.productRowSelection.selectData = selectedRows;
                barTender.state.productRowSelection.selectedRowKeys = selectedRowKeys;
            }
        },
        customerRowSelection: {
            selectData: [],
            selectedRowKeys: [],
            type: 'checkbox',
            onChange: (selectedRowKeys, selectedRows) => {
                console.log('customerRowSelection ---> '+selectedRowKeys)
                console.log('customerRowSelection ---> '+selectedRows)
                barTender.state.customerRowSelection.selectData = selectedRows;
                barTender.state.customerRowSelection.selectedRowKeys = selectedRowKeys;
            }
        },
        rowRadioSelection: {
            selectData: [],
            selectedRowKeys: [],
            type: 'radio',
            onChange: (selectedRowKeys, selectedRows) => {
                barTender.state.selectTemplateName = selectedRows[0].templateName;
                barTender.mutations.queryBinding(selectedRows[0].templateName);
                barTender.state.rowRadioSelection.selectData = selectedRows;
                barTender.state.rowRadioSelection.selectedRowKeys = selectedRowKeys;
            }
        },
        templateInfoDataSource: [],
        templateListDataSource: [],
        delTemplateInfo: [],
        bindType: "1",
    },
    mutations: {
        //选择绑定类型
        setBindType(state, val) {
            state.bindType = val
            //切换时 重新加载已经绑定的数据
            if (state.rowRadioSelection.selectData[0] !== undefined) {
                barTender.mutations.queryBinding(state.rowRadioSelection.selectData[0].templateName);
            }
        },
        clearData(state) {
            state.addOrEditTemplate.value = "";
        },
        clearTemplate(state) {
            state.delData = [];
            state.addData = [];
            state.editData = [];
            state.updateTemplateData = [];
        },
        setTemplateVisible(state, val) {
            state.templateVisible = val;
        },
        clearParameters(state) {
            state.addOrEditTemplate = {
                id: "",
                type: "",
                parameter: "",
                value: "",
            }
        },
        edit(state, record) {
            state.templateVisible = true;
            state.addOrEditTemplate = {
                key: record.id,
                id: record.id,
                type: record.type,
                parameter: record.bartenderPara,
                value: record.value,
            };
            state.addOrEdit = "EDIT";
        },
        del(state, record) {
            state.delFlg = 'del';
            state.delData.push(...state.templateInfoDataSource.filter(item => item.id == record.id));
            state.templateInfoDataSource = state.templateInfoDataSource.filter(item => item.id !== record.id);
        },
        addAndEditHandleOk(state) {
            if (state.addOrEdit === 'ADD') {
                let addTemplate = {
                    key: Math.random(),
                    id: Math.random(),
                    type: state.addOrEditTemplate.type,
                    bartenderPara: state.addOrEditTemplate.parameter,
                    value: state.addOrEditTemplate.value,
                };
                let i = 0;
                state.templateInfoDataSource.forEach(s => {
                    if ( s.bartenderPara == addTemplate.bartenderPara) {
                        i++;
                        return;
                    }
                });
                if (i > 0) {
                    message.error(i18n.t('dataSame'));
                    return;
                }

                state.addData = [...state.addData, addTemplate];
                state.templateInfoDataSource = [...state.templateInfoDataSource, addTemplate];
            } else if (state.addOrEdit === 'EDIT') {
                let editTemple =
                    {
                        key: state.addOrEditTemplate.key,
                        id: state.addOrEditTemplate.id,
                        type: state.addOrEditTemplate.type,
                        bartenderPara: state.addOrEditTemplate.parameter,
                        value: state.addOrEditTemplate.value,
                    };
                let source = [];
                let i = 0;
                source.push(...state.templateInfoDataSource.filter(item => item.id != editTemple.id));
                source.forEach(s => {
                    if ( s.bartenderPara == editTemple.bartenderPara) {
                        i++;
                        return;
                    }
                });
                if (i > 0) {
                    message.error(i18n.t('dataSame'));
                    return;
                }
                for (let i = 0; i < state.templateInfoDataSource.length; i++) {
                    const o = state.templateInfoDataSource[i];
                    if (o.id === editTemple.id) {
                        o.id = editTemple.id
                        o.type = editTemple.type
                        o.bartenderPara = editTemple.bartenderPara
                        o.value = editTemple.value
                    }
                }
                state.editData = [];
                state.editData = [...state.editData, editTemple];
                state.addOrEdit = "";
            } else {
                let updateTemplate = {
                    key: Math.random(),
                    id: Math.random(),
                    type: state.addOrEditTemplate.type,
                    bartenderPara: state.addOrEditTemplate.parameter,
                    value: state.addOrEditTemplate.value,
                };
                let i = 0;
                state.templateInfoDataSource.forEach(s => {
                    if ( s.bartenderPara == updateTemplate.bartenderPara) {
                        i++;
                        return;
                    }
                });
                if (i > 0) {
                    message.error(i18n.t('dataSame'));
                    return;
                }

                state.updateTemplateData = [...state.updateTemplateData, updateTemplate];
                state.templateInfoDataSource = [...state.templateInfoDataSource, updateTemplate];
            }
            state.templateVisible = false;
        },
        handleCancel(state) {
            barTender.mutations.clearParameters(state);
            if (!state.addOrEdit == "ADD") {
                state.addOrEdit = "";
            }
            state.templateVisible = false;
        },
        addParameter(state) {
            if (state.templateList.name === "") {
                message.warn(i18n.t('chooseName'));
                return;
            }
            barTender.mutations.clearParameters(state);
            state.templateVisible = true;
        },
        newTemplate(state) {
            state.addOrEdit = "ADD";
            state.templateNameDisabled = false;
            state.templateInfoDataSource = [];
            state.templateList.name = "";
            barTender.mutations.clearParameters(state);
            barTender.mutations.clearTemplate(state);
        },
        save(state) {
                if (state.templateList.name === "") {
                    message.warn(i18n.t('temName'));
                    return;
                } else if (state.templateInfoDataSource.length === 0) {
                    message.warn(i18n.t('temPara'));
                    return;
                }
                let inTrx = {
                    trxId: "bisTemplate",
                    actionFlg: "I",
                    templateName: state.templateList.name,
                    addData:[...state.addData],
                    editData:[...state.editData],
                    delData:[...state.delData],
                    updateTemplateData:[...state.updateTemplateData],
                };
                SendMsg.sendPostTrx(inTrx).then(res => {
                    let data = res.data;
                    if ('0000000' === data.rtnCode) {
                        barTender.mutations.queryTemplate(state);
                        barTender.mutations.clearTemplate(state);
                        barTender.state.templateInfoDataSource = [];
                        barTender.state.templateNameDisabled = true;
                        message.success(i18n.t('success'))
                        state.addOrEdit = "";
                        barTender.mutations.clearParameters(state);
                        barTender.mutations.queryTemplateName(state.templateList.name);
                    }
                });

        },
        bindingHandleOk(state) {
            state.bindingVisible = false
        },
        bindingHandleCancel(state) {
            state.bindingVisible = false
            state.binding = {
                templateName: "",
                product: "",
                customerCode: "",
            };
            state.rowRadioSelection.selectedRowKeys = [];
            state.customerRowSelection.selectedRowKeys = [];
            state.productRowSelection.selectedRowKeys = [];
        },
        bindingClick(state) {
            state.bindingVisible = true;
            barTender.mutations.queryBindingData(state,"bind");
        },
        queryTemplate(state) {
            state.rowSelection.selectedRowKeys = [];
            state.templateInfoDataSource = [];
            state.templateList.name = "";
            let inTrx = {
                trxId: "bisTemplate",
                actionFlg: "Q",
                templateName: state.template.name,
            };
            SendMsg.sendPostTrx(inTrx).then(res => {
                let data = res.data;
                if ('0000000' === data.rtnCode) {
                    state.templateListDataSource = [];
                    state.templateListDataSource = data.oarA.map((item, index) => {
                        item.key = (index + 1);
                        return item;
                    });
                    barTender.state.templateNameDisabled = true;
                }
            });
        },

        queryTemplateName(name) {
            let inTrx = {
                trxId: "bisTemplate",
                actionFlg: "QN",
                templateName: name,
            };
            SendMsg.sendPostTrx(inTrx).then(res => {
                let data = res.data;
                if ('0000000' === data.rtnCode) {
                    barTender.state.templateInfoDataSource = [];
                    barTender.state.templateInfoDataSource.push(...data.oarB)
                }
            });
        },

        queryType(state) {
            let inTrx = {
                trxId: "bisData",
                actionFlg: "F",
                dataCate: "TYPE",
            };
            SendMsg.sendPostTrx(inTrx).then(res => {
                let data = res.data;
                if ('0000000' === data.rtnCode) {
                    state.typeList = [];
                    state.typeList .push(...data.oaryA) ;
                }
            });
        },
        queryValue(state) {
            let inTrx = {
                trxId: "bisData",
                actionFlg: "F",
                dataCate: "PRAR",
            };
            SendMsg.sendPostTrx(inTrx).then(res => {
                let data = res.data;
                if ('0000000' === data.rtnCode) {
                    state.valueList = [];
                    state.valueList .push(...data.oaryA) ;
                }
            });
        },

        queryDateValue(state) {
            let inTrx = {
                trxId: "bisData",
                actionFlg: "F",
                dataCate: "DATE",
            };
            SendMsg.sendPostTrx(inTrx).then(res => {
                let data = res.data;
                if ('0000000' === data.rtnCode) {
                    state.dateList = [];
                    state.dateList .push(...data.oaryA) ;
                }
            });
        },

        queryStringValue(state) {
            let inTrx = {
                trxId: "bisData",
                actionFlg: "F",
                dataCate: "PARM",
            };
            SendMsg.sendPostTrx(inTrx).then(res => {
                let data = res.data;
                if ('0000000' === data.rtnCode) {
                    state.stringValueList = [];
                    state.stringValueList .push(...data.oaryA) ;
                }
            });
        },
        delTemplate(state) {
            if (state.templateList.name == "") {
                message.warn(i18n.t('chooseName'));
                return;
            }
            let inTrx = {
                trxId: "bisTemplate",
                actionFlg: "DEL",
                templateName: state.templateList.name,
            };
            SendMsg.sendPostTrx(inTrx).then(res => {
                let data = res.data;
                if ('0000000' === data.rtnCode) {
                    barTender.mutations.queryTemplate(state);
                    barTender.mutations.clearTemplate(state);
                    barTender.state.templateNameDisabled = true;
                    barTender.state.templateInfoDataSource = [];
                    message.success(i18n.t('success'))
                }
            });
        },
        queryBindingData(state,type) {

            state.templateDataSource = [];
            state.productDataSource = [];
            state.userDataSource = [];
            barTender.state.productRowSelection.selectedRowKeys = [];
            barTender.state.customerRowSelection.selectedRowKeys = [];
            barTender.state.rowRadioSelection.selectedRowKeys = [];
            barTender.state.rowRadioSelection.selectData = [];
            let name = "";
            if (type == 'query') {
                name = state.binding.templateName;
            }else {
                name = state.templateList.name;
            }
            let inTrx = {
                trxId: "bisTemplate",
                actionFlg: "BIND_QUERY",
                templateName: name,
                produceName: state.binding.product,
                customerCode: state.binding.customerCode,
                type: type
            };
            SendMsg.sendPostTrx(inTrx).then(res => {
                let data = res.data;
                if ('0000000' === data.rtnCode) {
                    state.templateDataSource.push(...data.oarA)
                    state.productDataSource.push(...data.productConfigList)
                    state.userDataSource.push(...data.retLotList)
                    // barTender.state.productRowSelection.selectedRowKeys = data.productList;
                    // barTender.state.customerRowSelection.selectedRowKeys = data.customerList;
                    // if (data.oarA.length > 0 && name != "") {
                    //     barTender.state.rowRadioSelection.selectedRowKeys = [data.oarA[0].id];
                    // }
                    if (type == 'bind') {
                        state.binding.templateName = state.templateList.name;
                        // if (data.oarA.length > 0 && name != "") {
                        //     barTender.state.rowRadioSelection.selectedRowKeys = [data.oarA[0].id];
                        // }
                    }

                }
            });
        },
        saveBindingData(state) {
            if (barTender.state.rowRadioSelection.selectData.length == 0 && !barTender.state.selectTemplateName == "") {
                message.warn(i18n.t('isNotChoose'));
                return;
            }
            let bindProduct = []
            let noBindProduct = []
            let bindCustomer = []
            let noBindCustomer = []
            //绑定的product
            state.productRowSelection.selectData.forEach( s => {
                bindProduct.push(s.productName)
            })
            //解绑的product
            state.productDataSource.forEach( s => {
                if (!bindProduct.some(item => item === s.productName)){
                    noBindProduct.push(s.productName)
                }
            })
            //绑定的客户
            state.customerRowSelection.selectData.forEach( s => {
                bindCustomer.push(s.customerCode)
            })
            //解绑的客户
            state.userDataSource.forEach( s => {
                if (!bindCustomer.some(item => item === s.customerCode)){
                    noBindCustomer.push(s.customerCode)
                }
            })
            let inTrx = {
                trxId: "bisTemplate",
                actionFlg: "BIND_SAVE",
                templateName: state.selectTemplateName=="" ? state.binding.templateName:state.selectTemplateName,
                bindProduct: bindProduct,
                noBindProduct: noBindProduct,
                bindCustomer: bindCustomer,
                noBindCustomer: noBindCustomer,
                type: state.bindType
            };
            SendMsg.sendPostTrx(inTrx).then(res => {
                let data = res.data;
                if ('0000000' === data.rtnCode) {
                    message.success(i18n.t('success'))
                }
            });
        },
        queryBinding(name) {
            // barTender.state.productDataSource = [];
            // barTender.state.userDataSource = [];
            barTender.state.productRowSelection.selectData = [];
            barTender.state.customerRowSelection.selectData = [];
            barTender.state.productRowSelection.selectedRowKeys = [];
            barTender.state.customerRowSelection.selectedRowKeys = [];

            let inTrx = {
                trxId: "bisTemplate",
                actionFlg: "QUERY_BIND",
                templateName: name
            };
            SendMsg.sendPostTrx(inTrx).then(res => {
                let data = res.data;
                if ('0000000' === data.rtnCode) {
                    // barTender.state.productDataSource.push(...data.productConfigList);
                    // barTender.state.userDataSource.push(...data.retLotList);
                    barTender.state.productRowSelection.selectData.push(...data.productConfigList)
                    barTender.state.customerRowSelection.selectData.push(...data.retLotList)
                    barTender.state.productRowSelection.selectedRowKeys = data.productList;
                    barTender.state.customerRowSelection.selectedRowKeys = data.customerList;
                }
            });
        }

    },
    getters: {},
    action: {}
}
export default barTender;
